package com.kiln.juxingqs.ui

import android.content.Intent
import android.graphics.drawable.Drawable
import com.bumptech.glide.request.transition.Transition
import android.view.View
import androidx.core.graphics.drawable.toBitmap
import androidx.lifecycle.Observer
import client.global.commlib.comm_ui.bean.Areas
import client.global.commlib.toast.toast
import client.global.commlib.utils.StoreUtil
import client.global.commlib.utils.listFromPath
import client.global.commlib.widget.EmptyLayout
import com.bumptech.glide.request.target.CustomTarget
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.api.bean.UserInfo
import com.kiln.juxingqs.databinding.ActivityLookskxxnewBinding
import com.kiln.juxingqs.ui.vm.SetskxxNewViewModel
import com.tencent.ocr.sdk.common.OcrModeType
import com.tencent.ocr.sdk.common.OcrSDKConfig
import com.tencent.ocr.sdk.common.OcrSDKKit
import com.tencent.ocr.sdk.common.OcrType
import java.io.InputStream
import java.util.Timer
import java.util.TimerTask


class LookSkxxNewActivity : BaseMiddleActivity<SetskxxNewViewModel, ActivityLookskxxnewBinding>() {
    override fun getLayout(): Int {
        return R.layout.activity_lookskxxnew
    }

    var timerTask: TimerTask? = null
    var timer: Timer? = null
    override fun initViewModel(): SetskxxNewViewModel = createViewModel()
    override fun onDestroy() {
        timerTask?.let { it.cancel() }
        timer?.let { it.cancel() }
        super.onDestroy()
    }

    override fun initView() {
        binding.event = this

        binding.vm = viewModel

        StoreUtil.getBean<UserInfo>(this)?.let {
            viewModel.phone.value = it.phone
        }
        val secretId = "AKIDpV2LTTIvKVXcjmv007DKPNqGeu1IPWr3"
        val secretKey = "NVUG4pT1RuLgoiP7kEmU5aAymYFo9Set"

        val configBuilder: /*@@vlhvme@@*/OcrSDKConfig =
            OcrSDKConfig.newBuilder(secretId, secretKey, null)
                .setOcrType(OcrType.BankCardOCR) // 设置默认的业务识别，银行卡
                .setModeType(OcrModeType.OCR_DETECT_AUTO_MANUAL) // 自动识别模式
                .setReflectWarn(true) // 自动识别的时长ms
                .setAutoTimeout(20000)
                .setReshootWarn(true)
                .setCopyWarn(true)
                .setBorderCheckWarn(true)
                .build()
        // 初始化SDK
        // 初始化SDK
        OcrSDKKit.getInstance().initWithConfig(this, configBuilder)
    }

    override fun observerUI() {
        binding.emptyLayout.setOnClickListener {
            viewModel.callThree()
        }
        viewModel.dataThree.observe(this, {
            it?.let {
                if (it.isSuccess) {

                    it.data?.pay_list?.let {
                        if (it.size > 0) {
                            viewModel.realName.value = it.get(0).real_name
                            viewModel.bankName.value = it.get(0).bank_name
                            viewModel.bankCard.value = it.get(0).bank_card
                            viewModel.idcard.value = it.get(0).id_card
                            viewModel.id_card_type.value = it.get(0).id_card_type
                            viewModel.startDate.value = it.get(0).begin_date
                            viewModel.endDate.value = it.get(0).end_date
                            viewModel.phone.value = it.get(0).phone
                            viewModel.userEmail.value = it.get(0).email
                            viewModel.userAddr.value = it.get(0).detail_address


                            if (it.get(0).status == 1) {
                                binding.edBtn.visibility = View.VISIBLE
                            }

                            mImgLoader.load("https://res.heepay.com" + it.get(0).bank_front_img)
                                .into(object : CustomTarget<Drawable>() {
                                    override fun onResourceReady(
                                        resource: Drawable,
                                        transition: Transition<in Drawable>?
                                    ) {
                                        viewModel.bankcardFont.value = resource.toBitmap()
                                        // 图片加载完成后的处理逻辑
                                    }

                                    override fun onLoadCleared(placeholder: Drawable?) {
                                        // 可选的清除操作
                                    }
                                })
                            mImgLoader.load("https://res.heepay.com" + it.get(0).bank_back_img)
                                .into(object : CustomTarget<Drawable>() {
                                    override fun onResourceReady(
                                        resource: Drawable,
                                        transition: Transition<in Drawable>?
                                    ) {
                                        viewModel.bandcardBack.value = resource.toBitmap()
                                        // 图片加载完成后的处理逻辑
                                    }

                                    override fun onLoadCleared(placeholder: Drawable?) {
                                        // 可选的清除操作
                                    }
                                })
                            mImgLoader.load("https://res.heepay.com" + it.get(0).bank_owner_hold_img)
                                .into(object : CustomTarget<Drawable>() {
                                    override fun onResourceReady(
                                        resource: Drawable,
                                        transition: Transition<in Drawable>?
                                    ) {
                                        viewModel.bandcardHand.value = resource.toBitmap()
                                        // 图片加载完成后的处理逻辑
                                    }

                                    override fun onLoadCleared(placeholder: Drawable?) {
                                        // 可选的清除操作
                                    }
                                })
                            mImgLoader.load("https://res.heepay.com" + it.get(0).id_card_front_img)
                                .into(object : CustomTarget<Drawable>() {
                                    override fun onResourceReady(
                                        resource: Drawable,
                                        transition: Transition<in Drawable>?
                                    ) {
                                        viewModel.idcardFont.value = resource.toBitmap()
                                        // 图片加载完成后的处理逻辑
                                    }

                                    override fun onLoadCleared(placeholder: Drawable?) {
                                        // 可选的清除操作
                                    }
                                })
                            mImgLoader.load("https://res.heepay.com" + it.get(0).id_card_back_img)
                                .into(object : CustomTarget<Drawable>() {
                                    override fun onResourceReady(
                                        resource: Drawable,
                                        transition: Transition<in Drawable>?
                                    ) {
                                        viewModel.idcardBack.value = resource.toBitmap()
                                        // 图片加载完成后的处理逻辑
                                    }

                                    override fun onLoadCleared(placeholder: Drawable?) {
                                        // 可选的清除操作
                                    }
                                })
                            mImgLoader.load("https://res.heepay.com" + it.get(0).hold_id_card_front_img)
                                .into(object : CustomTarget<Drawable>() {
                                    override fun onResourceReady(
                                        resource: Drawable,
                                        transition: Transition<in Drawable>?
                                    ) {
                                        viewModel.idcardFontHand.value = resource.toBitmap()
                                        // 图片加载完成后的处理逻辑
                                    }

                                    override fun onLoadCleared(placeholder: Drawable?) {
                                        // 可选的清除操作
                                    }
                                })

                            val input: InputStream = resources.assets.open("c_areas.json")
                            val citys = input.listFromPath<Areas>()


                            it.get(0).manage_region_code?.let { prov_id ->

                                var shengshis = ""
                                var shengshiIds = ""

                                citys?.RECORDS?.let {

                                    val sheng = prov_id.substring(0, 2) + "0000"
                                    for (c in it) {
                                        if (c.id.equals(sheng)) {
                                            shengshis += c.name
                                            shengshiIds += c.id
                                        }
                                    }
                                    val shi = prov_id.substring(0, 4) + "00"
                                    for (c in it) {
                                        if (c.id.equals(shi)) {
                                            shengshis += c.name
                                            shengshiIds += c.id
                                        }
                                    }
                                    for (c in it) {
                                        if (c.id.equals(prov_id)) {
                                            shengshis += c.name
                                            shengshiIds += c.id
                                        }
                                    }
                                }

                                viewModel.qu.value = shengshis
                                viewModel.quIds.value = shengshiIds

                            }
                            it.get(0).bank_region_code?.let { area_id ->

                                var shengshis = ""
                                var shengshiIds = ""

                                citys?.RECORDS?.let {

                                    val sheng = area_id.substring(0, 2) + "0000"
                                    for (c in it) {
                                        if (c.id.equals(sheng)) {
                                            shengshis += c.name
                                            shengshiIds += c.id
                                        }
                                    }
                                    val shi = area_id.substring(0, 4) + "00"
                                    for (c in it) {
                                        if (c.id.equals(shi)) {
                                            shengshis += c.name
                                            shengshiIds += c.id
                                        }
                                    }
                                    for (c in it) {
                                        if (c.id.equals(area_id)) {
                                            shengshis += c.name
                                            shengshiIds += c.id
                                        }
                                    }
                                }

                                viewModel.quBank.value = shengshis
                                viewModel.quIdsBank.value = shengshiIds

                            }

                        }
                    }

                    binding.emptyLayout.visibility = View.GONE
                } else {
                    toast(it.message)
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })

        viewModel.idcardFont.observe(this, Observer {
            it?.let {
                binding.idcardFontImg.setImageBitmap(it)
            }
        })
        viewModel.idcardBack.observe(this, Observer {
            it?.let {
                binding.idcardBackImg.setImageBitmap(it)
            }
        })
        viewModel.callThree()


        viewModel.idcardFont.observe(this, Observer {
            it?.let {
                binding.idcardFontImg.setImageBitmap(it)
            }
        })
        viewModel.idcardBack.observe(this, Observer {
            it?.let {
                binding.idcardBackImg.setImageBitmap(it)
            }
        })
        viewModel.idcardFontHand.observe(this, Observer {
            it?.let {
                binding.idcardHandFontImg.setImageBitmap(it)
            }
        })
        viewModel.bankcardFont.observe(this, Observer {
            it?.let {
                binding.bankcardFontImg.setImageBitmap(it)
            }
        })
        viewModel.bandcardBack.observe(this, Observer {
            it?.let {
                binding.bankcardBackImg.setImageBitmap(it)
            }
        })
        viewModel.bandcardHand.observe(this, Observer {
            it?.let {
                binding.bankcardHandImg.setImageBitmap(it)
            }
        })
    }

    fun goEdti() {
        startActivity(Intent(this, EditSkxxNewActivity::class.java))
    }
    //身份证正面

    // 身份证反面

    // 扫描银行卡


}